package com.heima.behavior.listener;

import com.alibaba.fastjson.JSONObject;
import com.heima.behavior.service.IApFollowBehaviorService;
import com.heima.user.dto.FollowBehaviorDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

/**
 * @author: itheima
 * @create: 2022-01-20 15:02
 */
@Slf4j
@Service
public class FollowBehaviorListener {


    @Autowired
    private IApFollowBehaviorService followBehaviorService;


    /**
     * 监听到用户关注行为话题，将该行为数据录入关注行为表中
     *
     * @param record 参数
     */
    @KafkaListener(topics = "${topic.followBehaviorTopic}")
    public void handlerMsg(ConsumerRecord<String, String> record) {
        try {
            log.info("【行为微服务监听器：】：{}" + record.value());
            if (StringUtils.isNotBlank(record.value())) {
                //将监听到数据行为DTO转为对象
                FollowBehaviorDto followBehaviorDto = JSONObject.parseObject(record.value(), FollowBehaviorDto.class);
                followBehaviorService.saveFollowBehavior(followBehaviorDto);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("关注行为监听器异常：{}", e.getMessage());
        }
    }


}
